package com.cloudcentral.util;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Create by WEI
 * 2023/4/7 10:36
 */
public class Utils {
  /**
   * 反射赋值类型转换
   *
   * @param object
   * @param field
   * @param value
   * @throws IllegalAccessException
   */
  public static void reflectFieldValue(Object object, Field field, Object value) throws IllegalAccessException {
    field.setAccessible(true);
    Class<?> fieldType = field.getType();
    String valueString = value.toString();
    if (Byte.class.equals(fieldType)) field.set(object, Byte.valueOf(valueString));
    else if (Short.class.equals(fieldType)) field.set(object, Short.valueOf(valueString));
    else if (Integer.class.equals(fieldType)) field.set(object, Integer.valueOf(valueString));
    else if (Long.class.equals(fieldType)) field.set(object, Long.valueOf(valueString));
    else if (Float.class.equals(fieldType)) field.set(object, Float.valueOf(valueString));
    else if (Double.class.equals(fieldType)) field.set(object, Double.valueOf(valueString));
    else if (Boolean.class.equals(fieldType)) field.set(object, Boolean.valueOf(valueString));
    else if (Character.class.equals(fieldType)) field.set(object, valueString.charAt(0));
    else if (String.class.equals(fieldType)) field.set(object, valueString);
    else field.set(object, value);
    field.setAccessible(false);
  }

  /**
   * 读取异常描述
   *
   * @param throwable
   * @return
   */
  public static String errorMessage(Throwable throwable) {
    String message = StringUtils.isBlank(throwable.getMessage()) ? throwable.getLocalizedMessage() : throwable.getMessage();
    return StringUtils.isBlank(message) ? throwable.getCause() == null ? message : errorMessage(throwable.getCause()) : message;
  }

  /**
   * 随机字符串
   *
   * @param length
   * @return
   */
  public static String randomStr(int length) {
    String charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    StringBuilder sb = new StringBuilder(length);
    Random random = new Random();
    for (int i = 0; i < length; i++) {
      int randomIndex = random.nextInt(charSet.length());
      char randomChar = charSet.charAt(randomIndex);
      sb.append(randomChar);
    }
    return sb.toString();
  }

  /**
   * 32位MD5加密
   *
   * @param value
   * @return
   * @throws RuntimeException
   */
  public static String md5(String value) throws RuntimeException {
    try {
      MessageDigest messageDigest = MessageDigest.getInstance("MD5");
      byte[] md5ValueByteArray = messageDigest.digest(value.getBytes());
      StringBuffer stringBuffer = new StringBuffer();
      for (int i, offset = 0; offset < md5ValueByteArray.length; offset++) {
        i = md5ValueByteArray[offset];
        if (i < 0) i += 256;
        if (i < 16) stringBuffer.append("0");
        stringBuffer.append(Integer.toHexString(i));
      }
      return stringBuffer.toString().toUpperCase();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * 对象转Map
   *
   * @param obj
   * @return
   */
  public static Map<String, Object> objectToMap(Object obj) {
    Map<String, Object> map = new HashMap<>();
    Class<?> cls = obj.getClass();
    while (cls != null && cls != Object.class) {
      Field[] fields = cls.getDeclaredFields();
      for (Field field : fields) {
        field.setAccessible(true);
        String name = field.getName();
        Object value = null;
        try {
          value = field.get(obj);
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        }
        map.put(name, value);
      }
      cls = cls.getSuperclass();
    }
    return map;
  }

  /**
   * 过滤HTML标签
   *
   * @param htmlStr
   * @return
   */
  public static String removeHTMLTag(String htmlStr) {
    String newstr = htmlStr.replaceAll("<[.[^>]]*>", "");
    newstr = newstr.replaceAll(" ", "");
    return newstr.trim();
  }
}
